import unittest
from pygo1963.model.Board import Board, InvalidMoveError
from pygo1963.model.Move import Move
from pygo1963.model.Constants import WHITE_COLOR, BLACK_COLOR, EMPTY
from pygo1963.model.Vertex import Vertex


class BoardTest(unittest.TestCase):

    def setUp(self):

        self.board = Board(9)

    def test_make_move(self):
       
        self.assertEqual(self.board.content[1][1], EMPTY, 'position should be empty')
        
        self.board.make_move(Move(WHITE_COLOR, Vertex(1, 1)))
        
        self.assertEqual(self.board.content[1][1], WHITE_COLOR)

        self.board.make_move(Move(WHITE_COLOR, Vertex(1, 0)))
        
        self.assertEqual(self.board.content[1][0], WHITE_COLOR)
        
        self.assertRaises(InvalidMoveError, self.board.make_move, Move(WHITE_COLOR, Vertex(1, 0)))
        
    def test_undo_move(self):

        self.board.make_move(Move(BLACK_COLOR, Vertex(5, 5)))
        self.assertEqual(self.board.content[5][5], BLACK_COLOR)

        self.board.undo_move()
        
        self.assertEqual(self.board.content[5][5], EMPTY)
        
    def test_add_to_group(self):
        
        self.board.make_move(Move(WHITE_COLOR, Vertex(2, 2)))
        
        self.assertEqual(len(self.board.groups), 1)
        self.assertEqual(len(self.board.groups[0].stones), 1)
        
        self.board.make_move(Move(BLACK_COLOR, Vertex(2, 3)))
        
        self.assertEqual(len(self.board.groups), 2)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex(2, 1)))
        self.assertEqual(len(self.board.groups), 2)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex(1, 0)))
        self.assertEqual(len(self.board.groups), 3)
        
        #Fusion test
        self.board.make_move(Move(WHITE_COLOR, Vertex(1, 1)))
        self.assertEqual(len(self.board.groups), 2)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex(1, 2)))
        self.assertEqual(len(self.board.groups), 2)
        
    def test_set_winner_if_possible_simple(self):
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a2")))
        
        self.assertFalse(self.board.game_finished)
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b1")))
        
        self.assertTrue(self.board.game_finished)
        
        self.assertEqual(self.board.winner, BLACK_COLOR)
        
    def test_set_winner_if_possible_suicide_capture(self):
        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("a2")))        
        self.board.make_move(Move(BLACK_COLOR, Vertex.from_string("b1")))
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a3")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("b2")))
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("c1")))
        
        self.assertFalse(self.board.game_finished)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        
        self.assertTrue(self.board.game_finished)
        self.assertEqual(self.board.winner, WHITE_COLOR)
        
    def test_set_winner_if_possible_double_pass(self):
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.PASS()))
        
        self.assertFalse(self.board.game_finished)

        self.board.make_move(Move(BLACK_COLOR, Vertex.PASS()))
        
        self.assertFalse(self.board.game_finished)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.from_string("a1")))
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.PASS()))
        
        self.assertFalse(self.board.game_finished)
        
        self.board.make_move(Move(WHITE_COLOR, Vertex.PASS()))
        
        self.assertTrue(self.board.game_finished)
        self.assertEqual(self.board.winner, BLACK_COLOR)        
        
if __name__ == "__main__":
    unittest.main()